package com.gxuwz.business.action;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.gxuwz.business.entity.Medicine;
import com.gxuwz.business.entity.Standard;
import com.gxuwz.business.service.IMedicineService;
import com.gxuwz.business.service.IStandardService;
import com.gxuwz.util.Pager;
import com.gxuwz.util.UidUtils;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import net.sf.json.JSONObject;

public class StandardAction extends BaseAction implements Preparable,ModelDriven<Standard>{

	private static final long serialVersionUID = 7978289945844975496L;
	
	private static final String STANDARD_LIST = "/WEB-INF/page/standard/standard_list.jsp";
	private static final String STANDARD_ADD = "/WEB-INF/page/standard/standard_add.jsp";
	private static final String STANDARD_UPDATE = "/WEB-INF/page/standard/standard_update.jsp";
	private static final String STANDARD_CHOICE = "/WEB-INF/page/standard/standard_choice.jsp";
	
	private Standard standard;
	
	@Autowired
	private IStandardService standardService;
	
	private Pager<Standard> standardList;
	
	private List<Standard> standardLists;
	
	/**
	 * 打开药品标准单界面
	 * @return
	 */
	public String standardList(){
		forwardView = STANDARD_LIST;
		return SUCCESS;
	}
	
	/**
	 * 分页查询
	 */
	public String getByPager(){
		standardList = standardService.getByPager(standard, page, limit);
		JsonData = JSONObject.fromObject(standardList);
		return "JsonObject";
	}
	
	/**
	 * 伪删除标准信息
	 */
	@SuppressWarnings("deprecation")
	public String delete(){
		isSucceed = standardService.delete(standard);
		jsonString = jsonToString(isSucceed);
		return "JsonString";
		
	}
	
	/**
	 * 打开添加标准单界面
	 */
	public String openAdd(){
		forwardView = STANDARD_ADD;
		return SUCCESS;
	}
	
	/**
	 * 添加费用标准信息
	 */
	@SuppressWarnings("deprecation")
	public String add(){
		//随机生成挂号编号
		standard.setStandId(UidUtils.UIDD());
		standard.setStatus(0);
		isSucceed = standardService.add(standard);
		jsonString = jsonToString(isSucceed);
		return "JsonString";
	}
	
	
	/**
	 * 检测药品是否已存在
	 */
	@SuppressWarnings("deprecation")
	public String checkStandName(){
		isSucceed =  standardService.checkStandName(standard);
		jsonString = jsonToString(isSucceed);
		System.out.println(jsonString);
		return "JsonString";
	}
	
	/**
	 * 打开更新界面
	 */
	public String openUpdate(){
		standard = standardService.findById(standard);
		System.out.println(standard);
		forwardView = STANDARD_UPDATE;
		return SUCCESS;
	}
	
	/**
	 * 更新信息
	 */
	@SuppressWarnings("deprecation")
	public String update(){
		System.out.println(standard);
		isSucceed = standardService.update(standard);
		jsonString = jsonToString(isSucceed);
		return "JsonString";
		
	}
	
	public String getAll(){
		//一次性将所有的药品赛到分页数据里
		standardLists = standardService.getAll(Standard.class);
		Pager<Standard> pager = new Pager<Standard>(0, "", standardLists.size() ,standardLists);
		JsonData = JSONObject.fromObject(pager);
		return "JsonObject";
	}
	
	/**
	 * 打开住院费用选中弹出、窗
	 */
	public String openChoice(){
		forwardView = STANDARD_CHOICE;
		return SUCCESS;
	}
	
	
	@Override
	public Standard getModel() {
		return standard;
	}

	@Override
	public void prepare() throws Exception {
		if(null == standard){
			standard = new Standard();
		}
	}

	public Standard getStandard() {
		return standard;
	}
	
	
}
